/**
 * 登录
 * Created by KY on 2017/4/17.
 */
define(['ldfw/core','art','weui',
        'text!tpl/login.tpl',
        'comps/ajax','comps/session','comps/validate',
        'ldfw/ldlibs/base64','ldfw/ldlibs/md5','ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW,artTemplate,weui,tpl,ajax,session,validate,base64) {
        var LoginView = LDFW.Container.extend({
            tpl:tpl,
            className:'panel login-panel',
            initialize:function(){
                this.intervalKey = 'login-delay-key'
                if(is_custom_skin && is_custom_skin.trim()){
                    this.model.set('is_custom_skin',is_custom_skin)
                    this.model.set('co_no',co_no)
                }
                this.on('before-shown',function(){
                    this.model.set('is_custom_skin',is_custom_skin)
                    this.model.set('co_fixed_telephone',global_config.co_fixed_telephone)
                })
            },
            rendered: function () {
                var registerDelay = session.getItem(this.intervalKey)
                if(registerDelay){
                    this.disableSendCode()
                    this.$el.find('#sendCode').html('重新发送('+registerDelay+')');
                    this.startInterval(registerDelay)
                }
            },
            events:{
                'change #phone_number':'checkPhone',
                'click #sendCode':'sendCode',
                'click [name="passwordLogin"]':'changeToPasswordLoginHandler',
                'click [name="phoneLogin"]':'changeToPhoneLoginHandler',
                'click [name="register"]':'register',
                'click #phoneLogin':'phoneLoginSubmit',
                'click #pwdLogin':'pwdLoginSubmit'
            },
            checkPhone:function(event){
                const self = this
                validate.checkElement($(event.target),'mobile')
                    .then(function(){
                        $(event.target).removeClass('error')
                        self.enableSendCode()
                    },function(msg){
                        $.toptip(msg,'error')
                        $(event.target).addClass('error')
                        self.disableSendCode()
                    })
            },
            disableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendCode')
                $sendCodeBtn.addClass('weui-btn_disabled')
                $sendCodeBtn.prop('disabled',true)
            },
            enableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendCode')
                $sendCodeBtn.removeClass('weui-btn_disabled')
                $sendCodeBtn.removeAttr('disabled')
            },
            register:function(){
                LDFW.navigate('register')
            },
            phoneLoginSubmit:function(){
                const _this = this
                weui.form.validate('#phoneLoginForm',function(error) {
                    if(error){
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur',function(){
                            $(this).removeClass('error')
                        })
                    }else{
                        let phoneNumber = $('#phone_number').val(),
                            validCode = $('#valid_code').val(),
                            data = {
                                phone_number:phoneNumber,
                                valid_code:validCode
                            }
                        ajax.post('customerLogin.do',data).success(function(resp){
                            if (!resp.resultCode) {
                                let investor = _.omit(resp,'error_code','error_deal','error_info','error_level','error_prompt')
                                _this.authenticationCheck(investor);
                            } else {
                                $.alert(resp.resultMsg)
                            }
                        })
                    }
                })
                return false
            },
            pwdLoginSubmit:function(){
                const _this = this
                weui.form.validate('#pwdLoginForm',function(error) {
                    if(error){
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur',function(){
                            $(this).removeClass('error')
                        })
                    }else{
                        let cust_login_name = $('#cust_login_name').val(),
                            cust_password = $('#cust_password').val(),
                            data = {
                                cust_loginname:cust_login_name,
                            }
                        data.cust_passwd = hex_md5(cust_login_name+'#'+cust_password);
                        ajax.post('customerPassWordLogin.do',data).success(function(resp){
                            if (!resp.resultCode) {
                                let investor = _.omit(resp,'error_code','error_deal','error_info','error_level','error_prompt')
                                _this.authenticationCheck(investor);
                            } else {
                                $.alert(resp.resultMsg)
                            }
                        })
                    }
                })
                return false
            },

            changeToPhoneLoginHandler:function(e){
                $("#pwdLoginForm").hide()
                $('#phoneLoginForm').show()
                e.stopPropagation()
                e.preventDefault()
            },
            changeToPasswordLoginHandler:function(e){
                $("#pwdLoginForm").show()
                $('#phoneLoginForm').hide()
                e.stopPropagation()
                e.preventDefault()
            },

            startInterval:function(delay){
                const _this = this;
                this.disableSendCode()
                let interval = setInterval(function(){
                    if(delay >= 0){
                        session.setItem(_this.intervalKey,delay)
                        _this.$el.find('#sendCode').html('重新发送('+delay+')')
                        delay--
                    }else{
                        _this.removeInterval(interval)
                    }
                },1000)
            },
            removeInterval:function(interval){
                clearInterval(interval)
                session.removeItem(this.intervalKey)
                this.$el.find('#sendCode').html('获取验证码')
                this.enableSendCode()
            },

            sendCode:function() {
                const _this = this
                let number = 60,
                    interval = null,
                    phone_number = $('#phone_number').val()
                if (phone_number && phone_number.trim()) {
                    ajax.get('sendCusterPhoneLoginCheckCode.do',{phone_no:phone_number})
                        .success(function(resp){
                            if(resp.resultCode){
                                $.alert(resp.resultMsg,function(){
                                    _this.enableSendCode()
                                })
                            }else{
                                _this.startInterval(60)
                            }
                        })
                        .error(function(){
                            _this.removeInterval(interval)
                        })
                }else{
                    $.toptip('请输入手机号','warning')
                    _this.disableSendCode()
                }
            },

            authenticationCheck:function(cust){
                session.setCurrentUser(cust)
                if(!cust.cust_no || cust.cust_no=='0'){
                    LDFW.navigate('authentication')
                }else if(!cust.risk_accept||!cust.ques_invest_num){
                    LDFW.navigate('risk_assessment')
                }else{
                    let request_url;
                    if((request_url=session.getItem('request_url'))&&true){
                        LDFW.navigate(request_url)
                    }else{
                        LDFW.navigate('main')
                    }
                }
            }
        })
        return new LoginView()
})

